package 牛客剑指专题二刷.栈队列;

import java.util.Stack;

/**
 * 栈的压入、弹出序列
 * 法1：模拟出栈顺序，遍历pop，push；如果栈顶=pop就出栈，最后如果栈为空则true
 */
public class JZ31 {
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int idPop = 0;
        for(int i = 0; i < pushV.length; ++i){
            stack.push(pushV[i]);
            while (!stack.isEmpty() && stack.peek() == popV[idPop]){
                stack.pop();
                idPop++;
            }
        }
        return stack.isEmpty();
    }

}
